import sys

from PySide6.QtGui import QPainter
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PySide6.QtCharts import QChart, QPieSeries, QPieSlice, QChartView
from PySide6.QtCore import Qt, Slot

class CircularProgressBar(QWidget):
    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        # 创建图表
        self.chart = QChart()
        self.chart.setTitle("")
        self.chart.legend().hide()

        # 创建系列并设置初始值
        self.series = QPieSeries()
        self.series.append("Progress", 0)
        self.slice = self.series.slices()[0]

        # 设置进度条颜色
        self.slice.setBrush(Qt.darkBlue)
        self.slice.setPen(Qt.NoPen)

        # 添加系列到图表
        self.chart.addSeries(self.series)

        # 创建视图
        self.chart_view = QChartView(self.chart)
        self.chart_view.setRenderHint(QPainter.Antialiasing)

        # 布局
        layout = QVBoxLayout(self)
        layout.addWidget(self.chart_view)

        # 更新进度条的方法
        self.update_progress = Slot(int)(self.updateProgress)

    @Slot(int)
    def updateProgress(self, progress):
        self.slice.setValue(progress * 360)  # 将进度转换为角度
        self.slice.setLabel(f"{progress}%")
        self.slice.setLabelVisible(True)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    progress_bar = CircularProgressBar()
    progress_bar.show()

    # 示例进度更新
    for _ in range(101):  # 从0% 到 100%
        progress_bar.update_progress(_)
        app.processEvents()  # 更新界面

    sys.exit(app.exec_())
